package com.weic.manager.login.controller;

import com.weic.manager.controller.BaseController;
import com.weic.manager.login.service.LoginService;
import com.weic.manager.user.mapper_service.UserService;
import com.weic.manager.user.vo.UserVO;
import com.weic.manager.utils.WUtils;
import com.weic.manager.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 登入Controller
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController extends BaseController {

    @Autowired
    LoginService loginService;

    @Autowired
    UserService userService;

    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping("/loginInfo.json")
    @ResponseBody
    public Map<String,Object> loginInfo(HttpServletRequest request){
        String userID = request.getParameter("userID");
        UserVO userVO = new UserVO();
        userVO.setPkUser(userID);
        userVO.setUserName("weic");
        userVO.setUserAge(26);
        return genAjaxResponse(true,"请求成功！",userVO);
    }

    /**
     * 返回登入页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/login.html")
    public ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response){
        logger.info("--------------------------------进入登入页面--------------------------------");
        return new ModelAndView("/login.html");
    }

    /**
     * 登入方法
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/login.json")
    public @ResponseBody Map<String,Object> login(HttpServletRequest request,HttpServletResponse response){
        String loginName = request.getParameter("loginName");
        String password = request.getParameter("password");
        UserVO userVO = userService.selectByUserLoginName(loginName);
        if(WUtils.isEmpty(userVO)){
            return genAjaxResponse(false,"账号或密码不正确",null);
        }
        if(!WUtils.isEmpty(userVO.getUserPassword()) && !userVO.getUserPassword().equals(password)){
            return genAjaxResponse(false,"账号或密码不正确",null);
        }
        WebUtils.setLoginInfo(userVO);
        return genAjaxResponse(true,"登入成功!",null);
    }

    /**
     * 登出操作
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/logout.do")
    public @ResponseBody Map<String,Object> logout(HttpServletRequest request,HttpServletResponse response){
        // logout
        WebUtils.remove();
        // 返回登入页面
        try {
            response.sendRedirect("/login/loginPage.html");
        }catch (IOException e){
            logger.info("重定向到登入页失败");
        }
        return null;
    }

    @RequestMapping(value = "/registre.json")
    public @ResponseBody Map<String,Object> registre(HttpServletRequest request){
        String loginName = request.getParameter("loginName");
        if(WUtils.isEmpty(loginName) || WUtils.isEmpty(loginName.trim())){
            return genAjaxResponse(false,"账号不能为空",null);
        }
        loginName = loginName.trim();
        UserVO userVO = userService.selectByUserLoginName(loginName);
        if(!WUtils.isEmpty(userVO)){
            return genAjaxResponse(false,"账号已存在",null);
        }
        String userName = request.getParameter("userName");
        if(WUtils.isEmpty(userName) || WUtils.isEmpty(userName.trim())){
            return genAjaxResponse(false,"名称不能为空",null);
        }
        userName = userName.trim();
        String password = request.getParameter("password");
        if(WUtils.isEmpty(password) || WUtils.isEmpty(password.trim())){
            return genAjaxResponse(false,"密码不能为空",null);
        }
        password = password.trim();
        UserVO record = new UserVO();
        WUtils.setUUID(record);
        record.setUserName(userName);
        record.setUserLoginName(loginName);
        record.setUserPassword(password);
//        LoginInfo loginInfo = WebUtils.getLoginInfo();
        loginService.addUser(record);
        return genAjaxResponse(true,"request success！",record);
    }
}
